package cn.b.sky.mes.cm.service.impl;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.b.sky.mes.cm.dao.WCCapacityCoefficientDao;
import cn.b.sky.mes.cm.dao.WCStandardCapacityDao;
import cn.b.sky.mes.cm.model.WCCapacityCoefficient;
import cn.b.sky.mes.cm.model.WCStandardCapacity;
import cn.b.sky.mes.cm.model.WorkCenter;
import cn.b.sky.mes.cm.model.condition.WCStandardCapacityCondition;
import cn.b.sky.mes.cm.service.WCStandardCapacityService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("wcStandardCapacityService")
public class WCStandardCapacityServiceImpl implements WCStandardCapacityService {

  private static final Logger LOG = LoggerFactory.getLogger(WCStandardCapacityServiceImpl.class);

  @Resource
  WCStandardCapacityDao wcStandardCapacityDao;

  @Resource
  WCCapacityCoefficientDao wcCapacityCoefficientDao;

  @Override
  public List<WCStandardCapacity> selectWCStandardCapacityListByCondition(WCStandardCapacityCondition condition) {
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return wcStandardCapacityDao.selectWCStandardCapacityListByCondition(condition);
  }

  @Override
  public int selectWCStandardCapacityCountByCondition(WCStandardCapacityCondition condition) {
    return wcStandardCapacityDao.selectWCStandardCapacityCountByCondition(condition);
  }

  @Override
  public WCStandardCapacity getWCStandardCapacity(String workCenterCode, String siteCode) {
    if (StringUtils.isBlank(workCenterCode) || StringUtils.isBlank(siteCode)) {
      return null;
    }
    WCStandardCapacity wcStandardCapacity = wcStandardCapacityDao.getWCStandardCapacity(workCenterCode, siteCode);
    List<WCCapacityCoefficient> coefficients = wcCapacityCoefficientDao.selectWCCapacityCoefficientList(workCenterCode,
        siteCode);
    Collections.sort(coefficients);
    wcStandardCapacity.setCapacityCoefficients(coefficients);
    return wcStandardCapacity;
  }

  @Override
  public void insertWCStandardCapacity(WCStandardCapacity wcStandardCapacity) {
    if (StringUtils.isBlank(wcStandardCapacity.getWorkCenterCode())
        || StringUtils.isBlank(wcStandardCapacity.getSiteCode())) {
      return;
    }
    wcStandardCapacityDao.insertWCStandardCapacity(wcStandardCapacity);
    this.insertCoefficients(wcStandardCapacity);
  }

  @Override
  public void updateWCStandardCapacity(WCStandardCapacity wcStandardCapacity) {
    if (StringUtils.isBlank(wcStandardCapacity.getWorkCenterCode())
        || StringUtils.isBlank(wcStandardCapacity.getSiteCode())) {
      return;
    }
    wcStandardCapacityDao.updateWCStandardCapacity(wcStandardCapacity);
    wcCapacityCoefficientDao.deleteAllWCCapacityCoefficient(wcStandardCapacity.getWorkCenterCode(),
        wcStandardCapacity.getSiteCode());
    this.insertCoefficients(wcStandardCapacity);
  }

  @Override
  public BatchJobReport deleteWCStandardCapacity(String workCenterCode, String siteCode) {
    if (!StringUtils.isBlank(workCenterCode) && !StringUtils.isBlank(siteCode)) {
      String message = workCenterCode;
      String cause = "";
      boolean success = true;
      try {
        wcCapacityCoefficientDao.deleteAllWCCapacityCoefficient(workCenterCode, siteCode);
        wcStandardCapacityDao.deleteWCStandardCapacity(workCenterCode, siteCode);
      } catch (Exception e) {
        LOG.error("{}", e);
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        success = false;
        String errorMsg = e.getMessage();
        if (DatabaseUtils.isConstraintsError(errorMsg)) {
          //外键删除已存在提示
          errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(),
              "dataBaseOperationPkFail.M")
              + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "notDelete.M");
        }
        cause = errorMsg;
      }
      return new BatchJobReport(message, cause, success, SecurityContextUtil.getLanguageCode(),SecurityContextUtil.getSiteCode());
    } else {
      return new BatchJobReport("", "id is null", false, SecurityContextUtil.getLanguageCode(),SecurityContextUtil.getSiteCode());
    }
  }

  private void insertCoefficients(WCStandardCapacity wcStandardCapacity) {
    List<WCCapacityCoefficient> coefficients = wcStandardCapacity.getCapacityCoefficients();
    String siteCode = wcStandardCapacity.getSiteCode();
    String workCenterCode = wcStandardCapacity.getWorkCenterCode();
    int ordinal = 0;
    for (WCCapacityCoefficient coefficient : coefficients) {
      if (coefficient.getLowerQuantity() != null || coefficient.getUpperQuantity() != null) {
        coefficient.setOrdinal(ordinal++);
        coefficient.setSiteCode(siteCode);
        coefficient.setWorkCenterCode(workCenterCode);
        wcCapacityCoefficientDao.insertWCCapacityCoefficient(coefficient);
      }
    }
  }

  @Override
  public List<WorkCenter> selectUnusedWorkCenterList(String workCenterCode, String siteCode) {
    return wcStandardCapacityDao.selectUnusedWorkCenterList(workCenterCode, siteCode);
  }

  @Override
  public WorkCenter getUnusedWorkCenter(String workCenterCode, String siteCode) {
    return wcStandardCapacityDao.getUnusedWorkCenter(workCenterCode, siteCode);
  }

}
